package org.qy.star_server.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.qy.star_common.enums.ExchangeType;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author yinger
 * @description TODO
 * @date 2024/3/26 07:40
 **/
@Data
public class Exchange {
    public static class ExchangeGloballyUniqueIdentity {
        // 用户名
        private final String username;
        // 交换机名称
        private final String exchangeName;
        // 虚拟主机名称
        private final String virtualHostName;

        public ExchangeGloballyUniqueIdentity(String username, String exchangeName, String virtualHostName) {
            this.username = username;
            this.exchangeName = exchangeName;
            this.virtualHostName = virtualHostName;
        }

        @Override
        public boolean equals(Object object) {
            if (this == object) return true;
            if (object == null || getClass() != object.getClass()) return false;
            ExchangeGloballyUniqueIdentity that = (ExchangeGloballyUniqueIdentity) object;
            return Objects.equals(username, that.username) && Objects.equals(exchangeName, that.exchangeName) && Objects.equals(virtualHostName, that.virtualHostName);
        }

        @Override
        public int hashCode() {
            return Objects.hash(username, exchangeName, virtualHostName);
        }
    }

    // 交换机名称
    private String exchangeName;
    // 虚拟主机名称
    private String virtualHostName;
    // 交换机类型
    private ExchangeType exchangeType;
    private String exchangeTypeCN;
    // 是否持久化
    private boolean durable = false;
    // 是否自动删除
    private boolean autoDelete = false;
    // 扩展参数
    private Map<String, Object> arguments;
    // 是否删除（数据库逻辑删除）
    private int isDeleted = 0;
    private String username;

    public Exchange(
            String username,
            String virtualHostName,
            String exchangeName,
            ExchangeType exchangeType,
            boolean durable,
            boolean autoDelete,
            Map<String, Object> arguments) {
        this.username = username;
        this.virtualHostName = virtualHostName;
        this.exchangeName = exchangeName;
        this.exchangeType = exchangeType;
        this.durable = durable;
        this.autoDelete = autoDelete;
        this.arguments = arguments;
    }

    public static Exchange getQueryExchange(String username, String virtualHostName, String exchangeName) {
        return new Exchange(username, virtualHostName, exchangeName, null, false, false, null);
    }

    public static Exchange getInsertExchange(String username, String virtualHostName, String exchangeName, ExchangeType exchangeType, boolean durable, boolean autoDelete, Map<String, Object> arguments) {
        return new Exchange(username, virtualHostName, exchangeName, exchangeType, durable, autoDelete, arguments);
    }

    public Exchange() {
    }


    /*MyBatis Mapper*/
    public String getArguments() {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "";
        }
    }

    public void setArguments(String arguments) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            this.arguments = mapper.readValue(arguments, new TypeReference<HashMap<String, Object>>() {
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public Object getArguments(String key) {
        return arguments.get(key);
    }

    public void setArguments(String k, Object v) {
        Exchange exchange = new Exchange();
        arguments.put(k, v);
    }


}
